package com.tom.poolsocket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 线程池版网络编程
 *
 * @author zx
 * @date 2024-09-24 21:03
 */
public class Server {
    private static ExecutorService executorService;

    static {
        executorService = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), 20,120L,
                TimeUnit.SECONDS,new ArrayBlockingQueue<Runnable>(1000));
    }
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(8080);
            Socket socket = null;
            while(true){
                socket = serverSocket.accept();
                executorService.execute(new ServerHandler(socket));
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    static class ServerHandler implements  Runnable {
        private Socket socket;

        public ServerHandler(Socket socket){
            this.socket = socket;
        }

        @Override
        public void run() {
            BufferedReader bufferedReader = null;
            try {
                bufferedReader = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
                while(true){
                    String line = bufferedReader.readLine();
                    if(line !=null){
                        System.out.println("line:"+line);
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                if(bufferedReader !=null){
                    try {
                        bufferedReader.close();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    }

}
